package com.cms.server.resolver.plugins;

import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.eai.data.JParamObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.form.io.FormDataResolverAdapter;
import com.efounder.sql.JStatement;

public class CMSPSHTFormItemDataCheck extends FormDataResolverAdapter {

	//初始化FormDataModel
	public void prepareSaveForm(JStatement stmt, EFFormDataModel model, JParamObject PO) throws Exception {
		EFDataSet                    billDataSet = model.getBillDataSet();
		EFRowSet                      itemRowSet;
		EFRowSet                  headItemRowSet;
		EFDataSet                    itemDataSet;
		EFDataSet                 newItemDataSet;
		java.util.List<String>    dataSetKeyList = new java.util.ArrayList<String>();

		if(billDataSet.getRowCount() > 0) {
			headItemRowSet = billDataSet.getRowSet(0);
			dataSetKeyList = headItemRowSet.getDataSetKeyList();
			if(dataSetKeyList.size() > 0) {
				itemDataSet = headItemRowSet.getDataSet(dataSetKeyList.get(0));
				newItemDataSet = EFDataSet.getInstance(itemDataSet.getTableName());
				newItemDataSet.setPrimeKey(new String[]{"F_BMBH"});
				for(int i = 0; i < itemDataSet.getRowCount(); i++){
			    	itemRowSet = itemDataSet.getRowSet(i);
			    	if(newItemDataSet.getRowSet(itemRowSet.getString("F_BMBH", "")) == null) {
			    		newItemDataSet.addRowSet(itemRowSet);
			    	}
			    }
				headItemRowSet.removeDataSet(itemDataSet.getTableName());
				headItemRowSet.setDataSet(itemDataSet.getTableName(), newItemDataSet);
			}
		}
	}
}
